/**
 * Created by zn on 17-4-6.
 * email: 312073339@qq.com
 */
'use strict';
import nodemailer from 'nodemailer'
import setting from '../setting'

const transporter = nodemailer.createTransport({
    host: setting.email.host, // 主机
    secureConnection: true, // 使用 SSL
    port: setting.email.port, // SMTP 端口
    auth: {
        user: setting.email.user, // 账号
        pass: setting.email.password // 密码
    }
});
export default class captcha {
    sendEmail (email, randomNumber) {
        let mailOptions = {
            from: 'zn', // sender address
            to: email, // list of receivers
            subject: ' 验证码', // Subject line
            text: '您好，您的验证码是'+randomNumber+'。10分钟内有效' // plaintext body
        };
        return new Promise((resolve, reject) => {
            transporter.sendMail(mailOptions, (error, info) => error? reject(error): resolve(info));
        })
    }
    createCaptcha () {
        return Math.floor(Math.random() * (999999 - 100000 + 1) + 100000)
    }
    getCaptcha (email, type) {
        return new Promise((resolve, reject) => {
            global.redis.get('captcha-' + email + '-' + type, (err, r) => err? reject(err): resolve(r))
        })
    }
    getCaptchaTTL (email, type) {
        return new Promise((resolve, reject) => {
            global.redis.ttl('captcha-' + email + '-' + type, (err, r) => err? reject(err): resolve(r))
        })
    }
    delCaptcha (email, type) {
        global.redis.del('captcha-' + email + '-' + type)
    }
}